class Solution:
    def intervalIntersection(self, firstList: list, secondList: list) -> list:
        res = []
        i1, i2 = 0, 0
        while i1 < len(firstList) and i2 < len(secondList):
            interval1 = firstList[i1]
            interval2 = secondList[i2]
            s1, e1 = interval1[0], interval1[1]
            s2, e2 = interval2[0], interval2[1]
            if e1 < s2:
                i1 += 1
            elif s1 > e2:
                i2 += 1
            else:
                res.append([max(s1, s2), min(e1, e2)])
                if e1 > e2:
                    i2 += 1
                elif e2 > e1:
                    i1 += 1
                else:
                    i1 += 1
                    i2 += 1

        return res
